//
//  JSEditTextView.m
//  JigSaw
//
//  Created by Slim Wang on 15/2/25.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSEditTextView.h"
#import "JSKeyboardView.h"
@interface JSEditTextView ()

@property(nonatomic, copy)NSString* inputStr;
@property(nonatomic, strong)CAShapeLayer* dashLayer;

@end

@implementation JSEditTextView

- (void)textViewDidChange:(UITextView *)textView
{
    NSAttributedString* attributeText = self.attributedText;
    UIFont* font = self.font;
    CGRect rect = [attributeText boundingRectWithSize:CGSizeMake(self.frame.size.width - 8, CGFLOAT_MAX)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                              context:nil];
    if (rect.size.height >= self.frame.size.height && ![self.inputStr isEqualToString:@""]) {
        if (font.pointSize - 1 >= self.minimumFontSize) {
            UIFont* newFont = [font fontWithSize:font.pointSize - 1];
            self.font = newFont;
        }
    } else {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        paragraphStyle.alignment = self.textAlignment;
        NSDictionary *attributes = @{NSFontAttributeName : [font fontWithSize:font.pointSize + 1], NSParagraphStyleAttributeName : paragraphStyle};
        NSMutableAttributedString* tempStr = [[NSMutableAttributedString alloc] initWithString:attributeText.string attributes:attributes];
        CGRect newRect = [tempStr boundingRectWithSize:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                                  context:nil];
        if (font.pointSize < self.defaultFontSize
            && [self.inputStr isEqualToString:@""]
            && newRect.size.height <= self.frame.size.height) {
            UIFont* newFont = [font fontWithSize:font.pointSize + 1];
            self.font = newFont;
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    self.inputStr = text;
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.beginEditingBlock) {
        self.beginEditingBlock();
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self addDashBorder];
//    if (self.beginEditingBlock) {
//        self.beginEditingBlock();
//    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    [self removeDashBorder];
//    if (self.endEditingBlock) {
//        self.endEditingBlock();
//    }
}

- (void)addDashBorder
{
    if (!self.dashLayer) {
        self.dashLayer = [CAShapeLayer layer];
        CGMutablePathRef path = CGPathCreateMutable();
        CGRect frame = self.bounds;
        CGFloat cornerRadius = 0;
        //drawing a border around a view
        CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
        CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
        CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
        CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
        CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
        CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
        CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
        
        //path is set as the layer object's path
        self.dashLayer.path = path;
        CGPathRelease(path);
        self.dashLayer.backgroundColor = [[UIColor clearColor] CGColor];
        self.dashLayer.frame = frame;
        self.dashLayer.masksToBounds = NO;
        [self.dashLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
        self.dashLayer.fillColor = [[UIColor clearColor] CGColor];
        self.dashLayer.strokeColor = [[UIColor blackColor] CGColor];
        self.dashLayer.lineWidth = 0.5;
        self.dashLayer.lineDashPattern = @[@4, @4];
        self.dashLayer.lineCap = kCALineCapRound;
    }
    [self.layer addSublayer:self.dashLayer];
    self.layer.masksToBounds = NO;
}

- (void)removeDashBorder
{
    [self.dashLayer removeFromSuperlayer];
}
@end
